﻿#include "scripteditdialog.h"
#include "ui_scripteditdialog.h"

#include <QMessageBox>

ScriptEditDialog::ScriptEditDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ScriptEditDialog)
{
    ui->setupUi(this);

    m_CodeHighLighter = new CodeHighLighter();
    m_CodeHighLighter->setDocument(ui->plainTextEdit->document());

    m_DebugDialog = new DebugDialog(this);
    m_ScriptLogDialog = new ScriptLogDialog(this);

    //ui->textBrowser->setStyleSheet(QString::fromUtf8 ("background-color:black"));
    ui->tabWidget->clear();
    ui->tabWidget->addTab(m_ScriptLogDialog,QString::fromLocal8Bit("系统日志"));
    ui->tabWidget->addTab(m_DebugDialog,QString::fromLocal8Bit("脚本调试"));
}

void ScriptEditDialog::addLog(QString type,QString msg)
{
    if(msg.isEmpty() || type.isEmpty())
        return;

    QString content = msg;

    if(type == "waring")
        content = "<font color=green>" + msg + "</font>";
    else if(type == "info")
        content = "<font color=white>" + msg + "</font>";
    else if(type == "error")
        content = "<font color=red>" + msg + "</font>";
    else
        content = "<font color=blue>" + msg + "</font>";

    m_ScriptLogDialog->addLog(content);
}

ScriptEditDialog::~ScriptEditDialog()
{
    delete ui;

    delete m_CodeHighLighter;
    delete m_ScriptLogDialog;
    delete m_DebugDialog;
}

void ScriptEditDialog::addScriptFile(QString filename)
{
    if(filename.isEmpty())
        return;

    ui->listWidget->addItem(filename);
}

void ScriptEditDialog::clearScriptFile(void)
{
    ui->listWidget->clear();
}

void ScriptEditDialog::on_listWidget_itemDoubleClicked(QListWidgetItem *item)
{
    m_currentSelectedScriptFile = item->text();

    emit scriptfileitemDoubleClicked(item->text());
}

void ScriptEditDialog::addScriptcontent(QString content)
{
    if(content.isEmpty())
        return;

    ui->plainTextEdit->clear();
    ui->plainTextEdit->appendPlainText(content);
}

void ScriptEditDialog::on_pushButton_clicked()
{
    if(m_currentSelectedScriptFile.isEmpty() || ui->plainTextEdit->toPlainText().isEmpty())
    {
        QMessageBox::warning(this,QString::fromLocal8Bit("警告"),QString::fromLocal8Bit("脚本内容不能为空!"));
        return;
    }

    emit scriptfileupdate(m_currentSelectedScriptFile,ui->plainTextEdit->toPlainText());
}

void ScriptEditDialog::on_tabWidget_currentChanged(int index)
{
    if(index == 1)
    {
        m_DebugDialog->connectDebugServer();
    }
}
